<?php

    ##################################################
    #
    # Copyright (c) 2004-2006 OIC Group, Inc.
    # Written and Designed by James Hunt
    #
    # This file is part of Exponent
    #
    # Exponent is free software; you can redistribute
    # it and/or modify it under the terms of the GNU
    # General Public License as published by the Free
    # Software Foundation; either version 2 of the
    # License, or (at your option) any later version.
    #
    # GPL: http://www.gnu.org/licenses/gpl.txt
    #
    ##################################################
    /**
    * Change logs
    * Sep 3, 2011 - trungdt - Add autoloader for module, disable module preload :) speedup
    * Aug 9, 2012 - trungdt - Add database support to cache the path
    */
    if (!defined('EXPONENT')) exit('');

    if (phpversion() >= 5) {
        /* exdoc
        * The definition of this constant lets other parts
        * of the system know that the AutoLoader Subsystem
        * has been included for use.
        * @node Subsystems:Autoloader
        */
        define('SYS_AUTOLOADER',1);

        /* exdoc
        * In PHP5, the autoloader function will check these
        * directories when it tries to load a class definition
        * file.  Other parts of the system should append to this
        * directory as needed, in order to take full advantage
        * of autoloading
        * @node Subsystems:Autoloader
        */
        //$auto_dirs = array(BASE.'datatypes', BASE.'subsystems/forms', BASE.'subsystems/forms/controls');
        $auto_dirs = array(
            BASE.'datatypes', 
            BASE.'subsystems/forms', 
            BASE.'subsystems/forms/controls', 
            //BASE.'framework'
        );

        /* exdoc
        * This function overrides the default PHP5 autoloader,
        * and instead looks at the $AutoLoadDirs global to look
        * for class files.  This function is automatically
        * invoked in PHP5
        *
        * @param	string $class The name of the class to look for.
        * @node Subsystems:Autoloader
        */
        function __autoload($class) {
            
            // First, check if requested file is pointed to module or not
            if (is_readable(BASE."modules/{$class}/class.php")) {
                include_once(BASE."modules/{$class}/class.php");
                return;
            }
            else if (is_readable(BASE."framework/{$class}.php")) {
                include_once(BASE."framework/{$class}.php");
                return;
            }
            
            
            // In case not point to module, then we should check on database first
            global $db;
            /**
            * CREATE TABLE  `hasan_credit`.`kcredit_system_classcache` (
                `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
                `class_name` TEXT NOT NULL ,
                `class_path` TEXT NOT NULL
            ) ENGINE = MYISAM ;
            */
            if ($db)
            {
                $data = $db->selectObject("system_classcache","class_name = '{$class}.php'");
                if ($data)
                {
                    // found class
                    $file_path = $data->class_path."/".$data->class_name;
                    if (is_readable ($file_path))
                    {
                        include_once($file_path);
                        return;
                    }
                }
            }
            
            // in case can not find this record in database, then we should search on HDD
            global $auto_dirs;
            foreach ($auto_dirs as $auto_dir) {
                if (is_readable($auto_dir.'/'.$class.'.php')) {
                    include_once($auto_dir.'/'.$class.'.php');
                    return;
                }
            }
        }
    } else {
        define('SYS_AUTOLOADER',2);
        if (is_readable(BASE.'datatypes')) {
            $dh = opendir(BASE.'datatypes');
            while (($file = readdir($dh)) !== false) {
                if (is_readable(BASE.'datatypes/'.$file) && substr($file,-4,4) == '.php') {
                    include_once(BASE.'datatypes/'.$file);
                }
            }
        }
    }

?>